// pages/test-on/test-on.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    randomList: [],
    index: 0,
    show: false,
    options: [],
    result: [],
    answer: {},
    value: 100,
    gradientColor: {
      '0%': '#ffd01e',
      '100%': '#ee0a24',
    },
    text: '50:00',
    timeSum: 3000,
    pageSize: 50,
    shows: false,
    totalScore: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
  },
  onChange(event) {
    this.setData({
      result: event.detail,
    });
    console.log(event.target.dataset.ids)
    let totals = this.data.randomList
    totals[this.data.index].userAnswers = event.detail
    this.setData({
      ["randomList["+event.target.dataset.ids+"].userAnswers"]:this.data.result,
      randomList: totals
    })
   
  },

  // 上一题
  lastQuestion() {
    let num = this.data.index;
    if (this.data.index <= 0) return
    this.setData({
      index: this.data.index - 1,
      options: JSON.parse(this.data.randomList[this.data.index - 1].options),
      result: this.data.answer[this.data.index-1].split('')
    })
    this.rightOrnot(num)
  },
  // 下一题
  nextQuestion() {
    let num = this.data.index;
    this.setData({
      index: this.data.index + 1,
      options: JSON.parse(this.data.randomList[this.data.index + 1].options)
    })
    // 判断是否选择答案
    if (this.data.answer[this.data.index + 1]) {
      this.data.answer[this.data.index] = this.data.result.sort().join('')
      this.setData({
        result: this.data.answer[this.data.index + 1].split('')
      })
    } else {
      this.data.answer[this.data.index] = this.data.result.sort().join('')
      this.setData({
        result: []
      })
    }
    // console.log(this.data.answer[this.data.index]);
    this.rightOrnot(num)
    console.log(this.data.randomList)
  },
  // 展示popup(答题进度)
  showPopup() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  // 跳转题号
  jumpQuestion(e) {
    console.log(e.target.dataset.idx);
    let a = e.target.dataset.idx;
    // console.log(this.data.index);
    console.log(this.data.randomList[a]);

    if (this.data.randomList[a].userAnswers) {
      this.setData({
        result: this.data.randomList[a].userAnswers
      })
    } else {
      this.setData({
        result: []
      })
    }
    let num = this.data.index
    this.setData({
      index: e.target.dataset.idx
    })
  },
  // 判断对错
  rightOrnot(num) {
    let totals = this.data.randomList//shuzu
    let items = totals[num] //num index
    // console.log(items);
    let answers = items.answers
    console.log(answers);

    if (this.data.randomList[num].userAnswers) {
      if (JSON.stringify(this.data.randomList[num].userAnswers.sort()) == answers) {
        items.isRight = true
      }
    }
    // console.log(items.isRight);

    this.setData({
      randomList: totals
    })



  },
  // 渲染列表获取数据
  getList() {
    wx.request({
      url: 'http://localhost:3001/questions/randList',
      data: {
        pageSize: this.data.pageSize
      },
      success: res => {
        let data = res.data.data
        this.setData({
          randomList: data,
          options: JSON.parse(data[this.data.index].options)

        })
        // console.log(data[this.data.index]);
      }
    })

  },
  // 得分
  getSore() {
    let totals = this.data.randomList;
    let zero = 0;
    let score = 100 / totals.length;
    totals.forEach(item => {
      if (item.userAnswers) {
        if (item.answers == JSON.stringify(item.userAnswers)) {
          zero += score
        }
      }
    })
    console.log(zero);
    this.setData({
      shows: true,
      totalScore: zero
    })


  },
  getUserInfo(event) {
    console.log(event.detail);
  },

  onClose() {
    this.setData({ show: false });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let timer = setInterval(() => {
      let seconds = this.data.timeSum - 1;

      let value = Math.floor(seconds / 30);

      let text = `${Math.floor(seconds / 60) < 10 ? '0' + Math.floor(seconds / 60) : Math.floor(seconds / 60)}  : ${seconds % 60 < 10 ? '0' + seconds % 60 : seconds % 60}`;

      this.setData({
        value,
        text,
        timeSum: seconds
      })

    }, 1000)

    setTimeout(() => {
      clearInterval(timer);
    }, 3000000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})